package gwc.domain

class MessageQueueTests extends GroovyTestCase {
	
	void setUp() {
		MessageQueue.instance.queue.clear()
		MessageQueue.instance.id = 0
	}
	void testAddAndGetFrom() {
		(1..10).each {i ->
			Thread.start {
				def user = new User(name : "$i", password : "$i")
				(1..5).each {
					MessageQueue.instance.add(new Message(user : user, content : "$user : $it"))
				}
			}
		}
		
		sleep(100)
		
		assertEquals 50, MessageQueue.instance.queue.size()
		
		(1..50).each {i ->
			def messages = MessageQueue.instance.getFrom(i)
			assertEquals 50 - i, messages.size()
			messages.eachWithIndex {message, index ->
				assertEquals "$message $i $index", message.id, i + index + 1
			}
		}		
	}
}